﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace TCCUIControls
{
    public partial class CommandListView : ListView
    {
        private int m_CurrentIndex = -1;
        private int m_Padding = 2;
        private Brush m_SelectedBrush = new SolidBrush(Color.SteelBlue);
        private Brush m_MesiE = new SolidBrush(Color.LightSkyBlue);
        private Brush m_MesiS = new SolidBrush(Color.LightGreen);
        private Brush m_MesiM = new SolidBrush(Color.LightSalmon);
        private Brush m_MesiI = new SolidBrush(Color.LightGray);
        private Brush m_MesiW = new SolidBrush(Color.LightPink);
        private Brush m_NullValue = new SolidBrush(Color.LemonChiffon);
        private Brush m_Brush = new SolidBrush(Color.Khaki);
        private Brush m_WhiteBrush = new SolidBrush(Color.White);
        private Brush m_TextBrush = new SolidBrush(Color.Black);
        private Pen m_Pen = new Pen(Color.Gray);

        public int CurrentIndex
        {
            get { return m_CurrentIndex; }
            set
            {
                m_CurrentIndex = value;
                if (value >=0 && value < Items.Count)
                    this.EnsureVisible(value);
            }
        }

        public CommandListView()
        {
            InitializeComponent();
            this.OwnerDraw = true;
        }

        protected override void OnDrawItem(DrawListViewItemEventArgs e)
        {
            base.OnDrawItem(e);

            Graphics g = e.Graphics;
            Rectangle rect = e.Bounds;
            Brush brush = m_WhiteBrush;

            bool isSelected = ((e.State & ListViewItemStates.Selected) == ListViewItemStates.Selected);

            if (isSelected)
                brush = m_SelectedBrush;

            if (e.ItemIndex == m_CurrentIndex)
                brush = m_Brush;

            if (e.Item.Tag != null)
            {
                if (e.Item.Text.Contains("Null Value"))
                    brush = m_NullValue;
                if (e.Item.Text.Contains("[E]"))
                    brush = m_MesiE;
                if (e.Item.Text.Contains("[S]"))
                    brush = m_MesiS;
                if (e.Item.Text.Contains("[M]"))
                    brush = m_MesiM;
                if (e.Item.Text.Contains("[I]"))
                    brush = m_MesiI;
                if (e.Item.Text.Contains("[W]"))
                    brush = m_MesiW;
            }
            g.FillRectangle(brush, rect);
            //g.DrawRectangle(m_Pen, rect);

            rect.X += m_Padding;
            rect.Y += (m_Padding - 1);
            rect.Width -= 2 * m_Padding;
            rect.Height -= 2 * (m_Padding - 1);

            brush = m_TextBrush;

            if (isSelected)
                brush = m_WhiteBrush;

            if (e.Item.Tag != null)
                g.DrawString(String.Format("{0:000} |{1}", Convert.ToInt32(e.Item.Tag), e.Item.Text), DefaultFont, brush, rect);
            else
                g.DrawString(e.Item.Text, DefaultFont, brush, rect);
        }

        private void CommandListView_SelectedIndexChanged(object sender, EventArgs e)
        {
            //m_CurrentIndex = this.SelectedIndices[0];
        }
    }
}
